/*************************************************************************
 *  Copyright © 2023-2030 FXB CO.,LTD. All rights reserved.
 *------------------------------------------------------------------------
 *  公司：DefaultCompany
 *  项目：UPMProject3
 *  文件：ComponentSearchPopup.cs
 *  作者：Administrator
 *  日期：2025/5/24 11:26:38
 *  功能：Nothing
*************************************************************************/

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;
using System;
using UnityEditor.IMGUI.Controls;

namespace CopyPasteComponents.Editor
{
    public class ComponentSearchPopup : PopupWindowContent
    {
        public float width = 232f; // 弹窗宽度
        public float height = 320f; // 弹窗高度

        private string searchText = "";
        private List<Type> allScriptTypes;
        private Vector2 scrollPos;

        private SearchField searchField;
        private GUIStyle btnStyle = new GUIStyle();
        private Type currentType;
        private Texture2D moveTexture = null;
        private Action<Type> selectCall;


        public ComponentSearchPopup(Action<Type> selectCall)
        {
            this.selectCall = selectCall;
        }

        public override Vector2 GetWindowSize()
        {
            return new Vector2(width, height);
        }

        public override void OnOpen()
        {
            allScriptTypes = GetDerivedTypes<Component>();

            searchField = new SearchField();
            searchField.SetFocus();
        }

        /// <summary>
        /// 创建一张纯色贴图
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="color"></param>
        /// <returns></returns>
        private Texture2D CreateTexture(int width, int height, Color color)
        {
            Texture2D texture = new Texture2D(width, height);
            for (int w = 0; w < width; w++)
            {
                for (int h = 0; h < height; h++)
                {
                    texture.SetPixel(w, h, color);
                }
            }
            texture.Apply();

            return texture;
        }

        public override void OnGUI(Rect rect)
        {
            //按钮样式设置
            btnStyle.alignment = TextAnchor.MiddleLeft;

            if (moveTexture == null)
            {
                moveTexture = CreateTexture(210, 25, new Color(0f, 0.5f, 1f, 0.5f));
            }

            EditorGUILayout.Space(2);

            EditorGUILayout.BeginHorizontal();

            searchText = searchField.OnToolbarGUI(searchText);

            EditorGUILayout.EndHorizontal();

            EditorGUILayout.LabelField("Search", new GUIStyle { alignment = TextAnchor.MiddleCenter, fontStyle = FontStyle.Bold, normal = new GUIStyleState { textColor = Color.white } });

            // 获取过滤后的结果
            var filteredList = FilterTypes(searchText);

            // 绘制列表
            scrollPos = EditorGUILayout.BeginScrollView(scrollPos, false, true, GUIStyle.none, GUI.skin.verticalScrollbar, GUI.skin.box);
            EditorGUILayout.BeginVertical();

            for (int i = 0; i < filteredList.Count; i++)
            {
                Type type = filteredList[i];

                Event e = Event.current;

                btnStyle.normal.background = null;
                btnStyle.normal.textColor = Color.white;

                if (type == currentType)
                {
                    btnStyle.normal.background = moveTexture;
                }

                Rect itemRect = GUILayoutUtility.GetRect(210, 25);


                if (GUI.Button(itemRect, type.Name, btnStyle))
                {
                    if (Event.current.button == 0)
                    {
                        selectCall?.Invoke(type);
                        editorWindow.Close(); // 关闭窗口
                    }
                }

                //鼠标经过控件
                if (e.type == EventType.MouseMove && itemRect.Contains(e.mousePosition))
                {
                    currentType = type;
                    Event.current.Use();
                }
            }

            EditorGUILayout.EndVertical();
            EditorGUILayout.EndScrollView();
        }


        List<Type> FilterTypes(string keyword)
        {
            return allScriptTypes
                .Where(t => t.Name.ToLower().Contains(keyword.ToLower()))
                .ToList();
        }

        List<Type> GetDerivedTypes<T>() where T : class
        {
            return AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(a => a.GetTypes())
                .Where(t => t.IsSubclassOf(typeof(T)) && !t.IsAbstract)
                .ToList();
        }
    }
}




